﻿using DbMapper.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Runtime.Remoting.Contexts;

namespace DbMapper.service
{
    public class DbMapperService
    {
        SQLHelper sqlHelper = new SQLHelper();
        DbInfo dbInfo = new DbInfo();
        public static bool ValidateDatabase(DbInfo dbInfo)
        {
            var connStrBuilder = new SqlConnectionStringBuilder();
            connStrBuilder.DataSource = dbInfo.Host;
            connStrBuilder.InitialCatalog = dbInfo.Name;
            connStrBuilder.UserID = dbInfo.User;
            connStrBuilder.Password = dbInfo.Password;
            using (SqlConnection connstr = new SqlConnection())
            {
                connstr.ConnectionString = connStrBuilder.ConnectionString;
                try
                {
                    connstr.Open();
                    return true;
                }
                catch  (Exception )
                {
                    connstr.Close();
                    return false ;
                } 
            }
        }
        public void Convert(IList<Mapper> mappers)
        {
            throw new NotImplementedException();
        }
        public SqlDataReader GetTableName(string sql)
        {
            string sqlstr = "select name from sysobjects where XTYPE='U'";
            return SQLHelper.ExecuteReader(sqlstr,sql);
        }
        //获取当前表的所有字段名
        public SqlDataReader GetTableField(string name)
        {
            string sqlstr = " Select name from syscolumns Where ID = OBJECT_ID('" + name + "')";
            return sqlHelper.ExecuteReader(sqlstr);
        }
        // 更改数据库
        public void UpdateDatabase(DbInfo dbInfo)
        {
            sqlHelper.UpdateConnectionString(dbInfo);
        }
        // 获取所有表明
        public SqlDataReader GetTableName()
        {
            string sqlstr = "select name from sysobjects where XTYPE='U'";
            return sqlHelper.ExecuteReader(sqlstr);
        }
        //连接数据库 
        public void ConnecteDatabase(DbInfo dbInfo)
        {
            sqlHelper.UpdateConnectionString(dbInfo);
        }
        //根据表明和字段名获取数据返回一个dt
        public DataTable GetFieldDataTable(string sourceTable, string TestName1)
        {
            string sqlstr = "Select " + TestName1 + " From " + sourceTable + "";
            return sqlHelper.getTABLE(sqlstr);
        }

        #region//根据表名获取数据返回一个dt
        public DataTable GetFieldDataTable(string sourceTable)
        {
            string sqlstr =" SELECT * FROM "+ sourceTable;
            return sqlHelper.getTABLE(sqlstr);
        }
        #endregion

        //根据表明和字段名插入数据返回一个dt
        public object InsertDate(string Table, string Field, DataTable dt)
        {
            int k = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sqlstr = "insert into " + Table + "(‘TestName2’) values ('" + dt.Rows[i]["TestName1"] + "')";
                //string sqlstr = "insert into " + Table + " TestName2 " +values ('" + dt.Rows[i]["UNIT_ID"] + "')";

                k = k + sqlHelper.ExecuteSql(sqlstr, "Data Source=.;Initial Catalog=testdatabase2;User ID=sa;Password=123");
            }
            return k;
        }

   

    }
}

